﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Caching;
using NetFramework.Diagnostics;

namespace NetFramework.Cache
{
    internal class AspNetCacheProvider : ICacheProvider
    {
        #region [  Fileds  ]

        private int defaultExpireSeconds = 60;
        private string keyPreFix = "AspNetCacheProvider_";
        private readonly string name = "Framework.Caching.AspNetCacheProvider";
        #endregion

        #region [  Constructors  ]

        public AspNetCacheProvider() { }

        public AspNetCacheProvider(string keyPreFix, int defaultExpireSeconds)
        {
            this.keyPreFix = keyPreFix;
            this.defaultExpireSeconds = defaultExpireSeconds;
        }

        #endregion

        #region [  ICacheProvider Members  ]

        public int DefaultExpireSeconds
        {
            get
            {
                return defaultExpireSeconds;
            }
        }

        public string KeyPreFix
        {
            get
            {
                return keyPreFix;
            }
        }

        public string Name
        {
            get
            {
                return name;
            }
        }

        /// <summary>
        /// Inserts an item into the System.Web.Caching.Cache object with a cache key
        /// to reference its location, using default values provided by the System.Web.Caching.CacheItemPriority
        /// enumeration.
        /// </summary>
        /// <param name="key"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        public void Insert(string key, object obj)
        {
            Insert(key, obj, this.defaultExpireSeconds);
        }

        public void Insert(string key, object obj, int seconds)
        {
            key = MakeUpInnerKey(key);

            if (seconds > 0)
            {
                HttpRuntime.Cache.Insert(key, obj, null, DateTime.Now.AddSeconds(seconds)
                        , TimeSpan.Zero, CacheItemPriority.Normal, null);
            }
            else
            {
                HttpRuntime.Cache.Insert(key, obj);
            }
        }

        public void Insert(string key, object obj, CacheDependency dep)
        {
            Ensure.ArgumentNotNull<CacheDependency>(dep, "CacheDependency dep");

            key = MakeUpInnerKey(key);
            HttpRuntime.Cache.Insert(key, obj, new System.Web.Caching.CacheDependency(dep.FileNames));
        }

        public object Get(string key)
        {
            return HttpRuntime.Cache.Get(MakeUpInnerKey(key));
        }

        public void Remove(string key)
        {
            HttpRuntime.Cache.Remove(MakeUpInnerKey(key));
        }
        #endregion

        #region [  Helpers  ]

        private string MakeUpInnerKey(string key)
        {
            return string.Concat(keyPreFix, key);
        }

        #endregion
    }
}
